<?php
    require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
    require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'check_admin_login.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_country.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_matches.php';
    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'html_generator.php';
    $title = 'Admin Panel';

$id = 0;
$name = "";
$description = "";
$start_date = "";
$end_date = "";
$country_code = "US";
$industry_id = 0;
$win_choice_id = 0;
$win_choice_name = "";
$is_cancel = 0;
$cancel_reason = 0;
$choices = array();
$action = "Add";
if (!empty($_GET['Action']) && $_GET['Action'] === 'Edit') {
    if (!empty($_GET['Id'])) {
        $id = $_GET['Id'];
    }
    $edit_item = api_matches::get_match_by_id($id);
    $id = $edit_item['id'];
    $name = $edit_item['name'];
    $description = $edit_item['description'];
    
    $start_date = $edit_item['date_start'];
    $date = date_create($start_date);
    $start_date = date_format($date, "Y/m/d H:i");
    
    
    $end_date = $edit_item['date_end'];
    $date = date_create($end_date);
    $end_date = date_format($date, "Y/m/d H:i");
    
    
    $country_code = $edit_item['country_code'];
    $industry_id = $edit_item['industry_id'];
    $win_choice_id = $edit_item['win_choice_id'];
    $win_choice_name = $edit_item['win_choice_name'];
    $is_cancel = $edit_item['is_cancel'];
    $cancel_reason = $edit_item['cancel_reason'];
    $choices = api_matches::get_match_choices_by_match_id($id);
    $action = "Update";
    
} else if (!empty($_POST)) {
    $id_save = filter_input(INPUT_POST, "hidd_id");
    $name_save = filter_input(INPUT_POST, "txt_name");
    $description_save = filter_input(INPUT_POST, "txt_description");
    $start_date_save = filter_input(INPUT_POST, "txt_start_date");
    $end_date_save = filter_input(INPUT_POST, "txt_end_date");
    $country_code_save = filter_input(INPUT_POST, "ddl_country");
    $industry_id_save = filter_input(INPUT_POST, "ddl_industry");
    $win_choice_id_save = filter_input(INPUT_POST, "ddl_win_choice");
    $choice_texts_save = array();
    if (!empty($_POST['txt_choices'])) {
        $choice_texts_save = $_POST['txt_choices'];
        $choice_texts_save = array_unique($choice_texts_save);
    }
    if (!empty($_POST['Action']) && ($_POST['Action'] === "Add" || $_POST['Action'] === "Update")) {
        $error_show = api_matches::validate_match_fields($name_save, 
                $description_save, $start_date_save, $end_date_save, 
                $country_code_save, $industry_id_save, $choice_texts_save);
        if (empty($error_show)) {
            if (api_matches::save_match($id_save, $name_save, 
                $description_save, $start_date_save, $end_date_save, 
                $country_code_save, $industry_id_save, $choice_texts_save, $win_choice_id_save)) {
                
                $success_info = "Added &lt;".$name_save."&gt; matches successfully.";
                if ($id_save > 0) {
                    $success_info = "Update &lt;".$name_save."&gt; matches successfully.";
                }
            }
        } else {
            $id = $id_save;
            $name = $name_save;
            $description = $description_save;
            $start_date = $start_date_save;
            $end_date = $end_date_save;
            $country_code = $country_code_save;
            $industry_id = $industry_id_save;
            $win_choice_id = $win_choice_id_save;
            $action = $_POST['Action'];
        }
    } else if (!empty($_POST['Action']) && $_POST['Action'] === "Delete") {
        $id_delete = filter_input(INPUT_POST, "hidd_delete_id");
        api_matches::delete_match($id_delete);
    }
    
}
    
?>

<!DOCTYPE html>
<!--[if lt IE 7]>
<html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>
<html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>
<html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->

<html>
<head>
    <?php require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php' ?>
    <link rel="stylesheet" type="text/css" href="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_CSS . 'jquery.datetimepicker.css' ?>"/>
    <script type="text/javascript">
        function add_more_choices() {
            var number = $('#txt_number_of_choices').val();
            $.ajax({
                type: "POST",
                url: "<?php echo LINK_ROOT . DIR_AJAX; ?>add_more_choices.php",
                data: "number=" + number,
                success: function (result) {
                    $("#more_choices").html(result);
                }
            });
        }
    </script>
</head>
<body>
<?php require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'header.php' ?>

<!-- Title -->
<div class="section section-black">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="calltoaction-wrapper">
                    <h3>Manage Matches</h3>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- End Title -->

<div class="ts-wrapper">
    <!-- Section Form -->
    <div class="section">
        <div class="container">
            <div class="row">
                <div class="col-md-offset-2 col-md-8">
                    <div class="service-wrapper">
                        <form action="?" method="POST" name="form_delete" id="form_delete" >
                            <input type="hidden" name="hidd_delete_id" value="0" />
                            <input type="hidden" name="Action" value="Delete" />
                        </form>
                        <div class="text-center ts-title ts-margin-bottom">Match Detail</div>
                        
                        <?php 
                        if (!empty($error_show)) {
                        ?>
                            <div class="alert-danger">
                                <?php echo $error_show; ?>
                            </div>
                        <?php
                        } else if (!empty($success_info)) {
                        ?>
                            <div class="alert-success">
                                <?php echo $success_info; ?>
                            </div>
                        <?php
                        } ?>
                        <br />
                        <form id="form_data" name="form_data" action="?" class="form-horizontal" method="POST" role="form">
                            
                            <?php
                                $disabled = "";
                                if (!empty($win_choice_id)) {
                                    $disabled = "disabled";
                                }
                                show_match_status_message($is_cancel, $cancel_reason, $win_choice_id, "WINNER SELECTED");
                            ?>
                            
                            <input type="hidden" name="hidd_id" value="<?php echo $id; ?>" />
                            
                            <div class="form-group">
                                <label for="txt_name" class="col-sm-3 control-label">Name <span class="ts-red">*</span></label>

                                <div class="col-sm-8">
                                    <input type="text" <?php echo $disabled; ?> class="form-control" name="txt_name" value="<?php echo $name; ?>" placeholder="Match Name" required="">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="txt_description" class="col-sm-3 control-label">Description
                                    <span class="ts-red">*</span></label>

                                <div class="col-sm-8">
                                    <textarea name="txt_description" <?php echo $disabled; ?> class="form-control" placeholder="Match Description" required=""><?php echo $description; ?></textarea>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="txt_start_date" class="col-sm-3 control-label">Start Date
                                    <span class="ts-red">*</span></label>

                                <div class="col-sm-8">
                                    <input type="text"  <?php echo $disabled; ?> class="form-control" name="txt_start_date" value="<?php echo $start_date; ?>" id="txt_start_date">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="txt_end_date" class="col-sm-3 control-label">End Date
                                    <span class="ts-red">*</span></label>

                                <div class="col-sm-8">
                                    <input type="text" <?php echo $disabled; ?> class="form-control" name="txt_end_date" value="<?php echo $end_date; ?>" id="txt_end_date">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="ddl_country" class="col-sm-3 control-label">Country
                                    <span class="ts-red">*</span></label>

                                <div class="col-sm-8">
                                    <select name="ddl_country" class="form-control"  <?php echo $disabled; ?>>
                                        <?php
                                            show_ddl_country($country_code);
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="ddl_country" class="col-sm-3 control-label">Industry
                                    <span class="ts-red">*</span></label>

                                <div class="col-sm-8">
                                    <select name="ddl_industry" class="form-control"  <?php echo $disabled; ?>>
                                        <?php
                                            show_ddl_industry($industry_id);
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                
                                <label for="ddl_win_choice" class="col-sm-3 control-label">Win Choice
                                    </label>

                                
                                <div class="col-sm-8">
                                    
                                    <select name="ddl_win_choice" class="form-control"  <?php echo $disabled; ?>>
                                        <?php
                                            show_ddl_win_choice($id, $win_choice_id);
                                        ?>
                                    </select>
                                    
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="txt_number_of_choices" class="col-sm-3 control-label">Number of Choices
                                    <span class="ts-red">*</span></label>

                                <div class="col-sm-8">
                                    <div class="input-group">
                                        <input type="text"  <?php echo $disabled; ?> id="txt_number_of_choices" value="<?php echo api_matches::get_number_of_choice_by_match_id($id) ?>" name="txt_number_of_choices" placeholder="1" class="form-control">
                                              <span class="input-group-btn">
                                                  <button onclick="add_more_choices();"  <?php echo $disabled; ?> class="btn btn-default" type="button">Add</button>
                                              </span>
                                    </div>
                                    <!-- /input-group -->
                                    <div id="more_choices">
                                        <?php
                                        foreach ($choices as $item) {
                                            show_ddl_choice($item['choice_name'], $disabled);
                                        }
                                        ?>
                                    </div>
                                </div>
                            </div>
                            <input type="hidden" name="Action" value="add">
                            <div class="form-group">
                                <div class="col-sm-12">
                                    <div id="action-button" <?php echo $disabled; ?> onclick="submit_data();" class="btn btn-default"><?php echo $action; ?></div>
                                    <a href="?"><div class="btn btn-orange">Cancel</div></a>
                                </div>
                            </div>
                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- End Section Form -->

    <!-- Section Table -->
    <div class="section">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="service-wrapper">
                        <table class="table">
                            <tr>
                                <th class="text-center">ID</th>
                                <th class="text-center">Name</th>
                                <th class="text-center">Closed</th>
                                <th class="text-center">Start Date</th>
                                <th class="text-center">End Date</th>
                                <th class="text-center">Country</th>
                                <th class="text-center">Industry</th>
                                <th class="text-center">Number of Choices</th>
                                <th class="text-center" colspan="2">Action</th>
                            </tr>
                            <?php
                            $list = api_matches::get_all_matches(); 
                            foreach ($list as $item) {
                            ?>
                            <tr>
                                <td><?php echo $item['id']; ?></td>
                                <td><?php echo $item['name'] ?></td>
                                <td>
                                    <?php 
                                    show_match_status_message($item['is_cancel'], $item['cancel_reason'], $item['win_choice_id'], "WINNER SELECTED");
                                    ?>
                                </td>
                                <td><?php echo show_datetime($item['date_start']); ?></td>
                                <td><?php echo show_datetime($item['date_end']); ?></td>
                                <td><?php echo $item['country_name']; ?></td>
                                <td><?php echo $item['industry_name']; ?></td>
                                <td><?php echo api_matches::get_number_of_choice_by_match_id($item['id']); ?></td>
                                <td>
                                    <a href="?Action=Edit&Id=<?php echo $item['id']; ?>">
                                        <img class="ts-action-icon center-block" src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_ICONS . 'edit.png' ?>" alt="edit">
                                    </a>
                                </td>
                                <td>
                                    <img onclick="confirmDelete('<?php echo $item['id']; ?>')" class="ts-action-icon center-block" src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_ICONS . 'delete.png' ?>" alt="delete">
                                </td>
                            </tr>
                            <?php
                            }
                            ?>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- End Section Table -->
</div>

<script type="text/javascript" src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_JS . 'jquery.datetimepicker.js' ?>"></script>
<script type="text/javascript">
    $('#txt_start_date').datetimepicker()
        .datetimepicker({value: '', step: 10});
    $('#txt_end_date').datetimepicker()
        .datetimepicker({value: '', step: 10});
</script>
<?php require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'footer.php' ?>
</body>
</html>